package com.sq.universe.system.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信工具类
 */
public class WxUtils {


    /**
     * 获取用户微信信息
     *
     * @param code
     * @return
     */
    public static Map getUserInfo(String code) {
        String appId = "*";
        String secret = "*";
//        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
//        String test = HttpUtil.createPost(url).execute().body();
        String authUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret="+secret+"&code="+code+"&grant_type=authorization_code";
        String authBody = HttpUtil.createPost(authUrl).execute().body();
        String token = JSON.parseObject(authBody).getString("access_token");
        String openid = JSON.parseObject(authBody).getString("openid");
        String userUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+token+"&openid="+openid+"";
        String userBody = HttpUtil.createPost(userUrl).execute().body();
        return JSON.parseObject(userBody,Map.class);
    }

}
